from django.db import models
from django.contrib.auth.models import User,AbstractBaseUser,PermissionsMixin,BaseUserManager
from django.contrib.auth.hashers import make_password
from shortuuidfield import ShortUUIDField

# Create your models here.

'''USER 的状态'''
class UserStatusChoices(models.IntegerChoices):
    ACTIVED = 1
    UNACTIVED = 2
    LOCKED = 3

'''重写 user.object.all()'''
class OAUserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, realname, email, password, **extra_fields):
        if not realname:
            raise ValueError("请设置姓名")
        email = self.normalize_email(email)
        user = self.model(realname=realname, email=email, **extra_fields)
        user.password = make_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, realname, email=None, password=None, **extra_fields):
        extra_fields.setdefault("is_staff", True)
        extra_fields.setdefault("is_superuser", False)
        return self._create_user(realname, email, password, **extra_fields)

    def create_superuser(self, realname, email=None, password=None, **extra_fields):
        extra_fields.setdefault("is_staff", True)
        extra_fields.setdefault("is_superuser", True)
        extra_fields.setdefault("status",UserStatusChoices.ACTIVED)

        if extra_fields.get("is_staff") is not True:
            raise ValueError("超级用户必须设置 is_staff=True.")
        if extra_fields.get("is_superuser") is not True:
            raise ValueError("超级用户必须设置 is_superuser=True.")

        return self._create_user(realname, email, password, **extra_fields)


'''重写 user 偶像'''
class OAUser(AbstractBaseUser, PermissionsMixin):

    ## 使用 uuid 确保 id唯一 且无序
    uid = ShortUUIDField(primary_key=True)
    realname = models.CharField(max_length=150,unique=False)
    email = models.EmailField(unique=True,blank=False)
    is_staff = models.BooleanField(default=True)
    status = models.IntegerField(choices=UserStatusChoices,default=UserStatusChoices.UNACTIVED)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    ## related_name 有一个 OADepartment 对象 怎么取到他下面的所有对象  User表中的外键 是 department表中的主键
    department = models.ForeignKey("OADepartment",null=True,on_delete=models.SET_NULL,related_name="staffs",related_query_name="staffs")

    ## user.object.all()
    objects = OAUserManager()

    EMAIL_FIELD = "email"
    USERNAME_FIELD = "email"  ## Django 中 默认是使用 username，password 鉴权 这个无比务必修改
    REQUIRED_FIELDS = ["realname","password"]


    def clean(self):
        super().clean()
        self.email = self.__class__.objects.normalize_email(self.email)

    def get_full_name(self):
        return self.realname

    def get_short_name(self):
        return self.realname

'''部门表'''
class OADepartment(models.Model):
    name = models.CharField(max_length=100)
    intro = models.CharField(max_length=200)
    ## leader  一个leader 对应一个user ,一个 user 对应一个 leader  related_query_name 跨表查询使用的
    leader = models.OneToOneField(OAUser,null=True,on_delete=models.SET_NULL,related_name="leader_department",related_query_name='leader_department')
    ## managerc 一一个
    manager = models.ForeignKey(OAUser,null=True,on_delete=models.SET_NULL,related_name="manager_departments",related_query_name='manager_departments')
